package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.ActivitesDTO;
import com.financing.wap.dto.ActivitiesWinListDTO;
import com.financing.wap.service.IActivitesService;
import com.financing.wap.service.IActivitiesWinListService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.ActivitiesWinListVo;
import com.financing.wap.web.controller.BaseController;

/**
 * 中奖记录列表
 * @author lisu
 *
 */
@Controller("activitiesWinListController")
@RequestMapping("/api/activitiesWinList")
public class ActivitiesWinListController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IActivitiesWinListService activitiesWinListServiceImpl;
	
	@Autowired
	private IActivitesService activitiesServiceImpl;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@ResponseBody
	public ResultMap activitiesWinList(HttpServletRequest request, ActivitiesWinListDTO activitiesWinListDTO,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query activitiesWinList begin.......");
			Map<String, Object> queryParams = new HashMap<String, Object>();
			//查询活动时间
			ActivitesDTO activitesDTO = activitiesServiceImpl.searchActivitesByCode("BWCSY");
			queryParams.put("pager", pager);
			queryParams.put("activitiesWinListDTO", activitiesWinListDTO);
			queryParams.put("createDateStar",activitesDTO.getActiviesStartTime());
			activitiesWinListDTO.setActiviesCode("BWCSY");
			int count = activitiesWinListServiceImpl.queryActivitiesWinListCount(queryParams);
			pager.setTotalCount(count);
			
			List<ActivitiesWinListVo> list;
			if(count>0){
				list = activitiesWinListServiceImpl.getActivitiesWinListList(queryParams);
				// 账户名加密
				for (int i = 0; i < list.size(); i++) {
					ActivitiesWinListVo bean = list.get(i);
					if (bean == null) {
						continue;
					}
					bean.setUserName(StringUtil.encryptCardNumber(bean
							.getUserName()));
				}
			}else{
				list = Collections.EMPTY_LIST;
			}
			result.setAttribute("list", list);
			result.setAttribute("page", pager);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setMessage("中奖信息查询成功");
			logger.info("query activitiesWinList end.......");
		}catch(Exception e){
			log.error("query activitiesWinList error!",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
}
